package com.qh.resourcehub.program.virtual.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qh.resourcehub.program.dto.VirtualDto;
import com.qh.resourcehub.program.entity.ApproveProcessDO;
import com.qh.resourcehub.program.entity.ApproveProcessRecordDO;
import com.qh.resourcehub.program.mapper.ApproveProcessMapper;
import com.qh.resourcehub.program.mapper.ApproveProcessRecordMapper;
import com.qh.resourcehub.program.mapper.VirtualMapper;
import com.qh.resourcehub.program.virtual.service.VirtualExamineService;
import com.qh.resourcehub.program.vo.VirtualVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Y6
 * @date: 2025/4/18 15:17
 */
@Service
public class VirtualExamineServiceImpl implements VirtualExamineService {
    @Autowired
    private VirtualMapper virtualMapper;

    @Autowired
    private ApproveProcessMapper approveProcessMapper;

    @Autowired
    private ApproveProcessRecordMapper approveProcessRecordMapper;
    @Override
    public List<VirtualVo> queryVirtualExamineData(VirtualDto virtualDto) {
        List<VirtualVo> result=new ArrayList<>();
        virtualDto.setInfoType("1");
        if (!StringUtils.isEmpty(virtualDto.getUserCode())) {
            result = virtualMapper.queryVirtualExamineData(virtualDto);
            for (VirtualVo virtualVo : result) {
                virtualVo.setExamineType("1");
            }
            List<VirtualVo> virtualVos = virtualMapper.queryVirtualExamineHistoryData(virtualDto);
            for (VirtualVo virtualVo : virtualVos) {
                virtualVo.setExamineType("2");
                result.add(virtualVo);
            }
        }
        return result;
    }

    @Override
    public List<VirtualVo> queryVirtualExamineDataNum(VirtualDto virtualDto) {
        List<VirtualVo> result=new ArrayList<>();
        virtualDto.setInfoType("1");
        if (!StringUtils.isEmpty(virtualDto.getUserCode())) {
            result = virtualMapper.queryVirtualExamineData(virtualDto);
            for (VirtualVo virtualVo : result) {
                virtualVo.setExamineType("1");
            }
        }
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationReject(VirtualDto virtualDto) {
        // 1. 驳回流程：将流程状态更新为“010”表示驳回
        LambdaUpdateWrapper<ApproveProcessDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                .set(ApproveProcessDO::getProcessStatus, "010");
        int updateCount = approveProcessMapper.update(null, updateWrapper);
        // 2. 修改当前审核流程记录表
        LambdaUpdateWrapper<ApproveProcessRecordDO> update=new LambdaUpdateWrapper<>();
        update.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                .eq(ApproveProcessRecordDO::getProcessStatus,virtualDto.getProcessStatus())
                .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg())
                .set(ApproveProcessRecordDO::getProcessStatus, "010");
        int upRecode=approveProcessRecordMapper.update(null, update);
        // 3. 返回操作结果（全部成功返回1，否则返回0）
        return (updateCount > 0 || upRecode > 0) ? 1 : 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationPass(VirtualDto virtualDto) {
        int result=0;
        if ("01".equals(virtualDto.getProcessStatus())){
            //待申请人所在单位领导审批
            //审批表状态修改为02
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper=new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "02");
            int updateProcess = approveProcessMapper.update(null,approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "01")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg());
            int updateOpinion =approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            //新增审批记录表的下一个审批用户记录信息
            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
            approveProcessRecordDO.setProcessStatus("02");
            int insertNext  = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 || insertNext > 0) ? 1 : 0;
        } else if ("02".equals(virtualDto.getProcessStatus()) ){
            //待信息中心相关科室审批
            //审批表状态修改为03
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper=new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "03");
            int updateProcess=approveProcessMapper.update(null,approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "02")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg());
            int updateOpinion =approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            //新增审批记录表的下一个审批用户记录信息
            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
            approveProcessRecordDO.setProcessStatus("03");
            int insertNext  = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 || insertNext > 0) ? 1 : 0;
        } else if ("03".equals(virtualDto.getProcessStatus())) {
            //待信息中心相关领导审批
            //审批表状态修改为03
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper=new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "04");
            int updateProcess=approveProcessMapper.update(null,approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "03")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg());
            int updateOpinion =approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            //新增审批记录表的下一个审批用户记录信息
            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
            approveProcessRecordDO.setProcessStatus("04");
            int insertNext  = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 || insertNext > 0) ? 1 : 0;
        } else if ("04".equals(virtualDto.getProcessStatus())) {
            //待观测与网络处领导审批
            //审批表状态修改为03
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper=new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "05");
            int updateProcess=approveProcessMapper.update(null,approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "04")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg());
            int updateOpinion =approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            //新增审批记录表的下一个审批用户记录信息
            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
            approveProcessRecordDO.setProcessStatus("05");
            int insertNext  = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 || insertNext > 0) ? 1 : 0;
        } else if ("05".equals(virtualDto.getProcessStatus())) {
            //待信息中心相关科室值班员开通
            //审批表状态修改为03
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper=new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "06");
            int updateProcess=approveProcessMapper.update(null,approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
//                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "05")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg())
                    .set(ApproveProcessRecordDO::getProcessStatus,  "06");
            int updateOpinion=approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
//            //新增审批记录表的下一个审批用户记录信息
//            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
//            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
//            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
//            approveProcessRecordDO.setProcessStatus("03");
//            result = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 ) ? 1 : 0;
        }
        return result;
    }


}
